<template>
  <div>
    <el-form :model="ruleForm" label-width="100px">
      <el-form-item label="账号" prop="username">
        <el-input
          type="username"
          v-model="ruleForm.username"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          type="password"
          v-model="ruleForm.password"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">
          提交
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios'
import { mapMutations } from 'vuex'
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
      },
    }
  },
  methods: {
    ...mapMutations(['set_token']),
    async submitForm() {
      const res = await axios.post('/api/login', this.ruleForm)
      if (res.data.code === 0) {
        console.log('res:', res.data.data)
        localStorage.setItem('token', res.data.data.token)
        // 给vuex中token赋值
        this.set_token(res.data.data.token)
        // 取出参数中的地址，如果有这个，就让用户跳转到这个地址
        const fullpath = this.$route.query.fullpath || '/layout/home'
        this.$router.push(fullpath)
      }
    },
  },
}
</script>

<style></style>
